<?php
require_once '_include.test.php5';
/////////////////////////////////////////////////////////////////////////////////////
class CronObject
	{
	public static function doInAMinute()
		{
		touch( FILE_ROOT . '__test_cron' );
		}
	}

/////////////////////////////////////////////////////////////////////////////////////
class TestOfCron extends UnitTestCase
	{
	function testFilename()
		{
		$cron = new PhpCron( 'minute' );
		$this->assertTrue( $cron->getFilename(), FILE_ROOT . '__cron_minute' );
		$cron = new PhpCron( 'hour' );
		$this->assertTrue( $cron->getFilename(), FILE_ROOT . '__cron_hour' );
		$cron = new PhpCron( 'day' );
		$this->assertTrue( $cron->getFilename(), FILE_ROOT . '__cron_day' );
		}
		
	function testNormal()
		{
		$cron = new PhpCron( 'second' );
		$cron->run();
		$time1 = filectime( $cron->getFilename() );
		sleep( 1 );
		$time2 = filectime( $cron->getFilename() );
		$this->assertTrue( $time1 == $time2 );
		$cron->run();
		$time2 = filectime( $cron->getFilename() );
		$this->assertFalse( $time1 == $time2 );
		}
		
	function testExtended()
		{
		$cron = new PhpCron( '3 second' );
		$this->assertTrue( $cron->getFilename(), FILE_ROOT . '__cron_3_second' );
		$cron->run();
		$time1 = filectime( $cron->getFilename() );
		sleep( 1 );
		$time2 = filectime( $cron->getFilename() );
		$this->assertTrue( $time1 == $time2 );
		$cron->run();
		sleep( 1 );
		$time2 = filectime( $cron->getFilename() );
		$this->assertTrue( $time1 == $time2 );
		$cron->run();
		sleep( 1 );
		$time2 = filectime( $cron->getFilename() );
		$this->assertTrue( $time1 == $time2 );
		$cron->run();
		sleep( 1 );
		$time2 = filectime( $cron->getFilename() );
		$this->assertFalse( $time1 == $time2 );
		$cron->run();
		}
		
	function testUpdate()
		{
		$test_file = FILE_ROOT . '__test_cron';
		if( file_exists( $test_file ) ) unlink( $test_file );
		CronObject::doInAMinute();
		$this->assertTrue( file_exists( $test_file ) );
		if( file_exists( $test_file ) ) unlink( $test_file );
		$this->assertFalse( file_exists( $test_file ) );

		$cron = new PhpCron( '3 second' );
		$cron->clean();
		$cron->register( 'CronObject', 'doInAMinute' );
		$cron->updateRegistered();
		$this->assertTrue( file_exists( $test_file ) );
		if( file_exists( $test_file ) ) unlink( $test_file );
		$this->assertFalse( file_exists( $test_file ) );

		$cron->run();
		$this->assertFalse( file_exists( $test_file ) );
		sleep(1);
		$cron->run();
		$this->assertFalse( file_exists( $test_file ) );
		sleep(1);
		$cron->run();
		$this->assertFalse( file_exists( $test_file ) );
		sleep(1);
		$cron->run();
		$this->assertTrue( file_exists( $test_file ) );
		
		$cron->clean();
		}
	}
?>